﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
    public class Express_WordBankRelDAL
    {
        private string DbName;
        public Express_WordBankRelDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string EW_ID, string WBK_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from SYS_Express_WordBankRel");
            strSql.Append(" where EW_ID=@EW_ID and WBK_ID=@WBK_ID  ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);
            oBase.AddInParameter(oCmd, "@WBK_ID", DbType.String, WBK_ID);
            if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
            {
                return true;
            }
            return false;
        }

        public bool Add_Express_WordBankRel(string ewrId, string ewId, string wbkId)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"
		   insert into dbo.SYS_Express_WordBankRel(EWR_ID,EW_ID,WBK_ID)values(@EWR_ID,@EW_ID,@WBK_ID)");
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

                oBase.AddInParameter(oCmd, "@EWR_ID", DbType.String, ewrId);
                oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, ewId);
                oBase.AddInParameter(oCmd, "@WBK_ID", DbType.String, wbkId);

                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception error)//唯一键
            {
                error.ToString();
                return false;
            }
        }

        public bool Add_Express_WordBankRel(List<SYS_Express_WordBankRel> bankrelList)
        {
            StringBuilder strSql = new StringBuilder();
            foreach (var bankrel in bankrelList)
            {
                strSql.Append(string.Format(@"insert into dbo.SYS_Express_WordBankRel(EWR_ID,EW_ID,WBK_ID) values('{0}','{1}','{2}');",
                                               bankrel.EWR_ID, bankrel.EW_ID, bankrel.WBK_ID));
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            int iResult = 0;
            try
            {
                iResult = oBase.ExecuteNonQuery(oCmd);
            }
            catch (Exception error)
            {
                AddLog.WriteLog(error, "");
            }
            return iResult > 0;
            //DbConnection oConn = DatabaseFactory.CreateDatabase(DbName).CreateConnection();
            //oConn.Open();
            //DbTransaction dbtran = oConn.BeginTransaction();
            //DbCommand oCmd = oConn.CreateCommand();
            //oCmd.Transaction = dbtran;
            //try
            //{
            //    foreach(var bankrel in bankrelList)
            //    {
            //oCmd.CommandText = string.Format(@"insert into dbo.SYS_Express_WordBankRel(EWR_ID,EW_ID,WBK_ID) values('{0}','{1}','{2}')",
            //                                   bankrel.EWR_ID, bankrel.EW_ID, bankrel.WBK_ID);
            //        oCmd.ExecuteNonQuery();
            //    }
            //    dbtran.Commit();
            //    oConn.Close();
            //    return true;
            //}
            //catch (Exception error)//唯一键
            //{
            //    error.ToString();
            //    dbtran.Rollback();
            //    oConn.Close();
            //    return false;
            //}
        }

        public DataTable Get_Express_WordBankRel(string EW_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select WBK_ID ,(select  WBK_Word from dbo.SYS_WordBank_KeyWords where WBK_ID=a.WBK_ID) as WBK_Word
            from SYS_Express_WordBankRel a
              where a.EW_ID=@EW_ID 
		  ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);


            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public bool Delete_Express_WordBankRel(string WBK_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
		 delete dbo.SYS_Express_WordBankRel where  WBK_ID=@WBK_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WBK_ID", DbType.String, WBK_ID);


            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        public bool AddList_Express_WordBankRel(string WBK_ID, string WordText)
        {
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            //            StringBuilder strSql = new StringBuilder();
            //            strSql.Append(@"
            //         insert into dbo.SYS_Express_WordBankRel
            //        select  newid(), EW_ID , MS_ID, @WBK_ID from 
            //        dbo.SYS_Express_Word_Process
            //        where EW_Content like'%'+@WordText+'%'
            //");
            //            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            //            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            //            oBase.AddInParameter(oCmd, "@WBK_ID", DbType.String, WBK_ID);
            //            oBase.AddInParameter(oCmd, "@WordText", DbType.String, WordText);

            //            if (oBase.ExecuteNonQuery(oCmd) > 0)
            //            {
            //                return true;
            //            }
            return false;
        }




    }
}
